<?php

namespace App\Store\Controllers\OldMaterial;

use App\Common\Services\CommonExcelExport;
use App\Store\Services\Financial;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\OldMaterial\TotalStock;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\OldMaterial\TotalStock as ValidateOldMaterialTotalStock;
use Maatwebsite\Excel\Facades\Excel;

/**
 * 旧料总库存
 */
class TotalStockController extends Controller
{
    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $request['cp_id'] = $cp_id;
        $list = TotalStock::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = TotalStock::where(['id' => $request['id']])->first();
        if ($one) $one = $one->toArray();
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
    }

    /**
     * 明细导出
     */
    public function doexport(Request $request)
    {
        $list = TotalStock::getList($request)['list'];

        $row = [
            ['table_header'=>'旧料总库存明细'],
            [
            'store_name' => '回收门店',
            'order_sncode' => '订单号',
            'create_at' => '回收时间',
            'stock_type' => '回收方式',
            'goods_sn_code' => '条码',
            'goods_category_id' => '旧料类别',
            'count' => '数量',
            'piece_weight' => '重量',
            'piece_weight_new' => '实重',
            'cf_no' => '证书号',
            'goods_brand_id' => '品牌',
            'goods_pricetype_id' => '价格类型',
            'main_stone_weigth' => '主石重',
            'main_stone_count' => '主石数',
            'other_stone_weigth' => '副石重',
            'other_stone_count' => '副石数',
            'color' => '颜色',
            'clarity' => '净度',
            'exchange_amount' => '换货金额',
        ]];

        $exceldata = [];
        foreach ($list as $val) {
            $exceldata[] = [
                'store_name' => !empty($val['storeinfo']['store_name']) ? $val['storeinfo']['store_name'] : '',
                'order_sncode' => $val['order_sncode'],
                'create_at' => date('Y-m-d H:i:s', $val['create_at']),
                'stock_type' => ($val['stock_type'] == 1) ? '按克' : '标价',
                'goods_sn_code' => $val['goods_sn_code'],
                'goods_category_id' => !empty($val['goods_category']['category_name']) ? $val['goods_category']['category_name'] : '',
                'count' => $val['count'],
                'piece_weight' => $val['piece_weight'],
                'piece_weight_new' => $val['piece_weight_new'],
                'cf_no' => $val['cf_no'],
                'goods_brand_id' =>  $val['goods_brand']['brand_name']?? '',
                'goods_pricetype_id' =>  $val['goods_pricetype']['pricetype_name']?? '',
                'main_stone_weigth' => $val['main_stone_weigth'],
                'main_stone_count' => $val['main_stone_count'],
                'other_stone_weigth' => $val['other_stone_weigth'],
                'other_stone_count' => $val['other_stone_count'],
                'color' => $val['color'],
                'clarity' => $val['clarity'],
                'exchange_amount' => $val['exchange_amount']/100,
            ];
        }

        $filename = '旧料总库存明细信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'return_supply_id', 'bank_card_number', 'paymoney', 'subject_id']);
            $Validate = new ValidateOldMaterialTotalStock('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        TotalStock::where('id', $req_data['id'])->update([
            'return_supply_id' => $req_data['return_supply_id'],
            'bank_card_number' => $req_data['bank_card_number'],
            'paymoney' => $req_data['paymoney'],
            'subject_id' => $req_data['subject_id'],
            'status' => 2,
            'update_at' => time()
        ]);

        $info = TotalStock::where('id', $req_data['id'])->first()->toArray();
        Financial::createMoneyLogByTotalstockin($cp_id,$info['store_id'],$req_data['paymoney'],$info['goods_sn_code'],$req_data['subject_id']);

        return response()->json(['code' => '000', 'msg' => '处理成功']);
    }
}
